package com.dqyt.lhq.service;

import com.alibaba.fastjson.JSON;
import com.dqyt.app.bean.*;
import com.dqyt.app.mongo.ReportLhqDao;
import com.dqyt.app.mongo.SampleLhqDao;
import com.dqyt.common.constant.Constant;
import com.dqyt.common.constant.Opt;
import com.dqyt.common.utils.BHUtil;
import com.dqyt.common.utils.StringUtil;
import com.dqyt.common.utils.UserInfoUtil;
import com.dqyt.khjc.bean.*;
import com.dqyt.khjc.swicth.SwitchConfig;
import com.dqyt.khjc.utils.IpUtil;
import com.dqyt.khjc.utils.TimeUtil;
import com.dqyt.lhq.bean.LhqQrCodeBean;
import com.dqyt.khjc.mapper.LhqQrCodeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.util.resources.ga.LocaleNames_ga;

import javax.servlet.http.HttpServletRequest;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class LhqSamplePcService {

    @Autowired
    private SampleLhqDao sampleLhqDao;


    @Autowired
    private LhqQrCodeMapper lhqQrCodeMapper;


    @Autowired
    private ReportLhqDao reportLhqDao;


    /**
     * 查询样品
     * @param request
     * @param ccbh 出厂编号
     * @param jlbh  记录编号
     * @param erweima  二维码
     * @return
     */
    public ResultMsg getPcSample2(HttpServletRequest request, String ccbh, String jlbh, String erweima){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);


            // 取得条件数据
            List<SampleLhq> samples = sampleLhqDao.getSample(ccbh, jlbh, erweima);
            SampleLhq sample = null;
            if(samples.size()>0){
                sample = samples.get(0); // 取得数据
                //在根据旧的数据吧二维码查询出来
                LhqQrCodeBean qrCodeBean =  lhqQrCodeMapper.getQrCodeBySbbh(sample.ccbh);
                if(null !=qrCodeBean){
                    sample.erweima =qrCodeBean.getErweima();
                    ret.setCode(ResultMsg.OLD_SAMPLE); // 旧的数据
                    ret.setData(sample);
                }else{
                    ret.setCode(ResultMsg.ERROR); // 旧的数据
                    ret.setMsg("二维码生成异常，请刷新重试");
                }
            }else{ //如果数据不存在
                //就创建二维码 并将二维码保存到本地
                ret.setCode(ResultMsg.NEW_SAMPLE_FP);
                sample = new SampleLhq(); // 新建数据
                sample.erweima = BHUtil.gethqErWeiMa(ccbh);
                ret.setData(sample);

                LhqQrCodeBean qrCodeBean =null;
                qrCodeBean =  lhqQrCodeMapper.getQrCodeBySbbh(ccbh);
                if(qrCodeBean == null){
                    qrCodeBean = new LhqQrCodeBean();
                    //将二维码放到数据库
                    qrCodeBean.setCcbh(ccbh);
                    qrCodeBean.setErweima(sample.erweima);
                    long nowTime = TimeUtil.getCurrentTimeMillis();
                    qrCodeBean.setAddTime(nowTime);
                    qrCodeBean.setAddUser(user.getUserId()+"");
                    qrCodeBean.setUpdateTime(nowTime);
                    qrCodeBean.setUpdateUser(user.getUserId()+"");
                    lhqQrCodeMapper.saveQrCode(qrCodeBean);
                }
            }
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



    /**
     * 查询样品
     * @param request
     * @param ccbh 设备编号
     * @param jlbh  标识编号
     * @param erweima  二维码
     * @return
     */
    public ResultMsg getPcSample5(HttpServletRequest request,String ccbh, String jlbh, String erweima){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            //数据验证
            if ( ccbh=="" || jlbh =="") {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("非法操作");
                return ret;
            }
            //通过标识编号去查询
            SampleLhq sample33 =   sampleLhqDao.getPcSampleByJlbh(jlbh);
            if(null != sample33){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("该标识编号已经被使用了");
                return ret;
            }
            ret.setCode(ResultMsg.NEW_SAMPLE_FP);
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



    /**
     * 新增样品信息
     * @param request
     * @param sampleJson
     * @return
     */
    public ResultMsg savePcLyqSample(HttpServletRequest request,String sampleJson){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);

            SampleLhq sample = JSON.parseObject(sampleJson, SampleLhq.class);
            sample.ccbh = sample.ccbh.trim();

            // 数据检查
            if (StringUtil.isEmpty(sample.jlbh) || StringUtil.isEmpty(sample.erweima)) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("数据错误！");
                return ret;
            }
            //如果设备编号里面包含了二维码协议头
            if(sample.ccbh.indexOf(Constant.EWM_HEAD) != -1){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("非法操作！");
                return ret;
            }


            // 保存数据
            // 取得是否存在旧的数据
            SampleLhq oldData = sampleLhqDao.getSampleByBH(sample.bh);
            if (oldData != null) {
                sample.sampleStatue = oldData.sampleStatue; // 样品状态记录
                sample.sampleStatue = oldData.sampleStatue;
            }else{
                sample.sampleStatue = 2;  // 没有已接收状态了  来就是检测中
            }
            // 设置一次数据编号
            sample.bh = sample.jlbh;
            sample.sjrq = TimeUtil.formatTime_tow(TimeUtil.currentTimeMillis());


            long time = TimeUtil.getCurrentTimeMillis();
            sample.setAddUser(user.getUserId() + "");
            sample.setAddTime(time);
            sample.setUpdateUser(user.getUserId() + "");
            sample.setUpdateTime(time);
            sample.setSysStatus(BaseBean.SYS_STATUS_OK);

            sampleLhqDao.save(sample);
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("保存成功");

        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     *
     * @param request
     * @param pageNum
     * @param pageSize
     * @param jlbh  记录编号
     * @param ccbh  标识编号
     * @param erweima  二维码
     * @return
     */
    public ResultMsg getLhqSampleListPage(HttpServletRequest request,Integer pageNum,Integer pageSize,String jlbh,String ccbh,String jlrIdStr,String erweima){
        List<Sample> sampleList = new ArrayList<Sample>();
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            if (pageNum != null && pageSize != null) {
                pageNum = (pageNum - 1) * pageSize;
            }
            long jlrId=0L;
            if(null != jlrIdStr && !"".equals(jlrIdStr)){
                jlrId = Long.parseLong(jlrIdStr);
            }

            //分页查询数据
            List<SampleLhq>  samples =  sampleLhqDao.getSampleListPage(pageNum, pageSize, jlbh,ccbh,jlrId, erweima);
            //查询总条数
            long  total = sampleLhqDao.getSampleListTotle(jlbh,ccbh,jlrId, erweima);
            RespPageBean bean = new RespPageBean();
            bean.setData(samples);
            bean.setTotal(total);
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(bean);
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;


    };



    public ResultMsg  deleteLhqSamp (HttpServletRequest request,String bh) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            long res = sampleLhqDao.deleteById(bh);
            if(res>0){
                ret.setCode(ResultMsg.SUCESS);
                ret.setMsg("操作成功");
            }else{
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("数据异常");
            }
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }




    /**
     *
     * @param request
     * @param jlbh
     * @return
     */
    public ResultMsg getPcSpBybsbh(HttpServletRequest request,String jlbh){
        List<Sample> sampleList = new ArrayList<Sample>();
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            SampleLhq sampleLhq= sampleLhqDao.getPcSampleByJlbh(jlbh);
            ret.setData(sampleLhq);
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("查询成");
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 保存H2S 对象
     * @param request
     * @param jlbh
     * @param h2sObjJson
     * @return
     */
    public ResultMsg savePcLyqH2sObj(HttpServletRequest request,String jlbh,String h2sObjJson){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);

            H2SObj h2SObj = JSON.parseObject(h2sObjJson, H2SObj.class);

            // 保存数据
            SampleLhq oldData = sampleLhqDao.getPcSampleByJlbh(jlbh);
            oldData.h2SObj = h2SObj;
            sampleLhqDao.save(oldData);


            //生成报告
            ReportLhq reportLhq = new ReportLhq();

            reportLhq.sampTYpe = 1;

            reportLhq.bh = oldData.jlbh+"_h2s";
            // 设备二维码
            reportLhq.erweima =  oldData.erweima;
            // 二维码的头部
            reportLhq.bsbhHrader =  oldData.bsbhHrader;
            // 出厂编号
            reportLhq.ccbh =  oldData.ccbh;
            // 记录编号  唯一
            reportLhq.jlbh =  oldData.jlbh;
            // 送检单位
            reportLhq.sjdw =  oldData.sjdw;
            // 仪器名称
            reportLhq.yqmc =  oldData.yqmc;
            // 制造单位
            reportLhq.zzdw =  oldData.zzdw;
            // 仪器型号
            reportLhq.yqxh =  oldData.yqxh;
            //传感器类型
            reportLhq.cgqlx =  oldData.cgqlx;
            // 记录人id
            reportLhq.jlrId =  oldData.jlrId;
            // 记录人
            reportLhq.jlr =  oldData.jlr;
            // 检定地址
            reportLhq.jddz =  oldData.jddz;
            //其他信息
            reportLhq.qt_xx = oldData.qt_xx;
            // 1是  -1 否   0默认没选
            reportLhq.isH2S =  oldData.isH2S;
            // 1是  -1 否   0默认没选
            reportLhq.isO2 =  oldData.isO2;
            // 1是  -1 否   0默认没选
            reportLhq.isKrq =  oldData.isKrq;
            // 1是  -1 否   0默认没选
            reportLhq.isCo =  oldData.isCo;
            reportLhq.jdyj1 =  oldData.jdyj1;
            reportLhq.jdyj2 =  oldData.jdyj2;
            reportLhq.jdyj3 =  oldData.jdyj3;
            reportLhq.jdyj4 =  oldData.jdyj4;
            reportLhq.jdzz1 =  oldData.jdzz1;
            reportLhq.jdzz2 =  oldData.jdzz2;
            reportLhq.jdzz3 =  oldData.jdzz3;
            reportLhq.jdzz4 =  oldData.jdzz4;
            reportLhq.sjrq =  oldData.sjrq;

            //设置检验日期
            reportLhq.jyrq =  TimeUtil.formatTime_tow(TimeUtil.currentTimeMillis());

            Date date2 = new Date(TimeUtil.currentTimeMillis());
            long year3 = TimeUtil.getDateBeforeYear(date2, 1);
            //下次检验日期
            reportLhq.xcjyrq = TimeUtil.formatTime_tow(year3);

            reportLhq.jyyId =user.getUserId()+"";
            reportLhq.jyy =user.getUserName();

            reportLhq.hyyId = "";
            reportLhq.hyy =  "";

            reportLhq.pzrId = "";
            reportLhq.pzr = "";


            H2SObjReport h2SObjReport = new H2SObjReport();
            //环境温度
            h2SObjReport.hjwd= h2SObj.hjwd;
            //环境湿度
            h2SObjReport.hjsd= h2SObj.hjsd;
            //外观与结构结果
            h2SObjReport.wgyjg_jg= h2SObj.wgyjg_jg;
            //外观与结构 备注
            h2SObjReport.wgyjg_bz= h2SObj.wgyjg_bz;
            //标志和标识 结果
            h2SObjReport.bzhbs_jg= h2SObj.bzhbs_jg;
            //标志和标识 备注
            h2SObjReport.bzhbs_bz= h2SObj.bzhbs_bz;
            //通电检查 结果
            h2SObjReport.tdjc_jg= h2SObj.tdjc_jg;
            //通电检查  备注
            h2SObjReport.tdjc_bz= h2SObj.tdjc_bz;
            //结缘电阻 结果
            h2SObjReport.jydz_jg= h2SObj.jydz_jg;
            //结缘电阻  备注
            h2SObjReport.jydz_bz= h2SObj.jydz_bz;
            h2SObjReport.m_bjgn= h2SObj.m_bjgn;//报警功能
            h2SObjReport.m_scbjz1= h2SObj.m_scbjz1; //实测报警值1
            h2SObjReport.m_scbjz2= h2SObj.m_scbjz2;//实测报警值2
            h2SObjReport.m_scbjz3= h2SObj.m_scbjz3;//实测报警值3
            h2SObjReport.m_bjz= h2SObj.m_bjz;//报警值
            h2SObjReport.m_swcz_bq1= h2SObj.m_swcz_bq1;//示值误差-标气浓度1
            h2SObjReport.m_swcz_bq2= h2SObj.m_swcz_bq2;//示值误差-标气浓度2
            h2SObjReport.m_swcz_bq3= h2SObj.m_swcz_bq3;//示值误差-标气浓度3
            h2SObjReport.m_swcz_x1= h2SObj.m_swcz_x1;
            h2SObjReport.m_swcz_x2= h2SObj.m_swcz_x2;
            h2SObjReport.m_swcz_x3= h2SObj.m_swcz_x3;
            h2SObjReport.m_swcz_y1= h2SObj.m_swcz_y1;
            h2SObjReport.m_swcz_y2= h2SObj.m_swcz_y2;
            h2SObjReport.m_swcz_y3= h2SObj.m_swcz_y3;
            h2SObjReport.m_swcz_z1= h2SObj.m_swcz_z1;
            h2SObjReport.m_swcz_z2= h2SObj.m_swcz_z2;
            h2SObjReport.m_swcz_z3= h2SObj.m_swcz_z3;
            h2SObjReport.m_swcz_pjz1= h2SObj.m_swcz_pjz1;//示值误差-算术平均值1
            h2SObjReport.m_swcz_pjz2= h2SObj.m_swcz_pjz2;//示值误差-算术平均值2
            h2SObjReport.m_swcz_pjz3= h2SObj.m_swcz_pjz3;//示值误差-算术平均值3
            h2SObjReport.m_swcz_swzc1= h2SObj.m_swcz_swzc1;//示值误差-示值误差1
            h2SObjReport.m_swcz_swzc2= h2SObj.m_swcz_swzc2;//示值误差-示值误差2
            h2SObjReport.m_swcz_swzc3= h2SObj.m_swcz_swzc3;//示值误差-示值误差3
            h2SObjReport.m_swcz_xdwc1= h2SObj.m_swcz_xdwc1;//示值误差-相对误差值1
            h2SObjReport.m_swcz_xdwc2= h2SObj.m_swcz_xdwc2;//示值误差-相对误差值2
            h2SObjReport.m_swcz_xdwc3= h2SObj.m_swcz_xdwc3;//示值误差-相对误差值3
            h2SObjReport.m_xysj_bq= h2SObj.m_xysj_bq;//响应时间- 标气浓度
            h2SObjReport.m_xysj_x1= h2SObj.m_xysj_x1;//
            h2SObjReport.m_xysj_x2= h2SObj.m_xysj_x2;//
            h2SObjReport.m_xysj_x3= h2SObj.m_xysj_x3;//
            h2SObjReport.m_xysj_pjz= h2SObj.m_xysj_pjz;//响应时间-算术平均值
            h2SObjReport.m_cfx_bq= h2SObj.m_cfx_bq;//重复性-标气浓度
            h2SObjReport.m_cfx_x1= h2SObj.m_cfx_x1;//
            h2SObjReport.m_cfx_x2= h2SObj.m_cfx_x2;//
            h2SObjReport.m_cfx_x3= h2SObj.m_cfx_x3;//
            h2SObjReport.m_cfx_x4= h2SObj.m_cfx_x4;//
            h2SObjReport.m_cfx_x5= h2SObj.m_cfx_x5;//
            h2SObjReport.m_cfx_x6= h2SObj.m_cfx_x6;//
            h2SObjReport.m_cfx_pjz= h2SObj.m_cfx_pjz;////重复性-算术平均值
            h2SObjReport.m_cfx_cfx= h2SObj.m_cfx_cfx;//重复性-重复性
            h2SObjReport.m_py_r= h2SObj.m_py_r;//漂移-仪器满量程
            h2SObjReport.m_py_x0= h2SObj.m_py_x0;//
            h2SObjReport.m_py_x1= h2SObj.m_py_x1;//
            h2SObjReport.m_py_x2= h2SObj.m_py_x2;//
            h2SObjReport.m_py_x3= h2SObj.m_py_x3;//
            h2SObjReport.m_py_x4= h2SObj.m_py_x4;//
            h2SObjReport.m_py_x5= h2SObj.m_py_x5;//
            h2SObjReport.m_py_x6= h2SObj.m_py_x6;//
            h2SObjReport.m_py_y0= h2SObj.m_py_y0;//
            h2SObjReport.m_py_y1= h2SObj.m_py_y1;//
            h2SObjReport.m_py_y2= h2SObj.m_py_y2;//
            h2SObjReport.m_py_y3= h2SObj.m_py_y3;//
            h2SObjReport.m_py_y4= h2SObj.m_py_y4;//
            h2SObjReport.m_py_y5= h2SObj.m_py_y5;//
            h2SObjReport.m_py_y6= h2SObj.m_py_y6;//
            h2SObjReport.m_py_y7= h2SObj.m_py_y7;//
            h2SObjReport.m_py_ldpy= h2SObj.m_py_ldpy;//漂移-零点漂移
            h2SObjReport.m_py_lcpy= h2SObj.m_py_lcpy;//漂移-量程漂移


            String bhgyy="";
            //判断是否合格
            String m_reportStatue="合格";
            if("不合格".equals(h2SObj.wgyjg_jg)){
                m_reportStatue = "不合格";
                bhgyy=bhgyy+"外观与结构"+",";
            }

            if("不合格".equals(h2SObj.bzhbs_jg)){
                m_reportStatue = "不合格";
                bhgyy=bhgyy+"标志与标识"+",";
            }

            if("不合格".equals(h2SObj.tdjc_jg)){
                m_reportStatue = "不合格";
                bhgyy=bhgyy+"通电检查"+",";
            }

//            if("不合格".equals(h2SObj.jydz_jg)){
//                m_reportStatue = "不合格";
//                bhgyy=bhgyy+"结缘电阻"+",";
//            }


            //果报警值 大于报警设定值  合格   否则不合格
            if(h2SObj.m_bjz<= h2SObj.m_bjsdz){
                m_reportStatue = "不合格";
                h2SObjReport.bjgn_pdjg = "不合格";
                bhgyy=bhgyy+"报警功能"+",";
            }else{
                h2SObjReport.bjgn_pdjg = "合格";
            }

            double mzdz=0;
            if(h2SObj.m_swcz_xdwc1>(-10) && h2SObj.m_swcz_xdwc1<10 && h2SObj.m_swcz_xdwc2>(-10) && h2SObj.m_swcz_xdwc2<10 && h2SObj.m_swcz_xdwc3>(-10) && h2SObj.m_swcz_xdwc3<10){
                h2SObjReport.swcz_pdjg = "合格";
                mzdz=h2SObj.m_swcz_xdwc1;

                if(h2SObj.m_swcz_xdwc2>mzdz){
                    mzdz = h2SObj.m_swcz_xdwc2;
                }

                if(h2SObj.m_swcz_xdwc3>mzdz){
                    mzdz = h2SObj.m_swcz_xdwc3;
                }
                //找出最大值
                h2SObjReport.szwc_zdz =mzdz+"";
            }else{
                if(h2SObj.m_swcz_swzc1>(-2) && h2SObj.m_swcz_swzc1<2  && h2SObj.m_swcz_swzc2>(-2) && h2SObj.m_swcz_swzc2<2 && h2SObj.m_swcz_swzc3>(-2) && h2SObj.m_swcz_swzc3<2){
                    h2SObjReport.swcz_pdjg = "合格";
                    mzdz=h2SObj.m_swcz_swzc1;
                    if(h2SObj.m_swcz_swzc2>mzdz){
                        mzdz = h2SObj.m_swcz_swzc2;
                    }

                    if(h2SObj.m_swcz_swzc3>mzdz){
                        mzdz = h2SObj.m_swcz_swzc3;
                    }
                    //找出最大值
                    h2SObjReport.szwc_zdz =mzdz+"";
                }else{
                    m_reportStatue = "不合格";
                    h2SObjReport.swcz_pdjg = "不合格";
                    bhgyy=bhgyy+"示值误差"+",";

                    if(h2SObj.m_swcz_swzc1<(-2) || h2SObj.m_swcz_swzc1>2){
                        mzdz =h2SObj.m_swcz_swzc1;
                    }

                    if(h2SObj.m_swcz_swzc2<(-2) || h2SObj.m_swcz_swzc2>2){
                        mzdz =h2SObj.m_swcz_swzc2;
                    }

                    if(h2SObj.m_swcz_swzc3<(-2) || h2SObj.m_swcz_swzc3>2){
                        mzdz =h2SObj.m_swcz_swzc3;
                    }


                    if(h2SObj.m_swcz_xdwc1<(-10) ||  h2SObj.m_swcz_xdwc1>10){
                        mzdz =h2SObj.m_swcz_xdwc1;
                    }

                    if(h2SObj.m_swcz_xdwc2<(-10) || h2SObj.m_swcz_xdwc2>10){
                        mzdz =h2SObj.m_swcz_xdwc2;
                    }

                    if(h2SObj.m_swcz_xdwc3<(-10) || h2SObj.m_swcz_xdwc3>10){
                        mzdz =h2SObj.m_swcz_xdwc3;
                    }

                    //找出最大值
                    h2SObjReport.szwc_zdz =mzdz+"";

                }
            }









            //响应时间判断
            if(h2SObj.m_xysj_pjz<=60){
                h2SObjReport.xysj_pdjg = "合格";
            }else{
                h2SObjReport.xysj_pdjg = "不合格";
                m_reportStatue = "不合格";
                bhgyy=bhgyy+"响应时间"+",";
            }

            //重复性 判断
            if(h2SObj.m_cfx_cfx <=2){
                h2SObjReport.cfx_pdjg = "合格";
            }else{
                h2SObjReport.cfx_pdjg = "不合格";
                m_reportStatue = "不合格";
                bhgyy=bhgyy+"重复性"+",";
            }

            //零点漂移
            if(h2SObj.m_py_ldpy <2 && h2SObj.m_py_ldpy>(-2)){
                h2SObjReport.ldpy_pdjg ="合格";
            }else{
                h2SObjReport.ldpy_pdjg ="不合格";
                m_reportStatue = "不合格";
                bhgyy=bhgyy+"零点漂移"+",";
            }

            //量程漂移
            if(h2SObj.m_py_lcpy <3 && h2SObj.m_py_lcpy>(-3)){
                h2SObjReport.lcpy_pdjg ="合格";
            }else{
                h2SObjReport.lcpy_pdjg ="不合格";
                m_reportStatue = "不合格";
                bhgyy=bhgyy+"量程漂移"+",";
            }




            if(m_reportStatue.equals("合格")){
                reportLhq.reportStatue= 1;
            }else if(m_reportStatue.equals("不合格")){
                reportLhq.reportStatue= 2;
               String  bhgyy_Str ="";
               if(!"".equals(bhgyy)){
                   bhgyy_Str=bhgyy.substring(0,bhgyy.length()-1);
               }
                h2SObjReport.bhgyy = bhgyy_Str+"不合格";
            }

            reportLhq.reportProgress =1;

            reportLhq.h2SObjReport = h2SObjReport;
            reportLhqDao.save(reportLhq);
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("保存成功");
        }catch (Exception e) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("未知的错误，请稍候再试！");
                e.printStackTrace();
            }
        return ret;
    }



    /**
     * 保存o2 对象
     * @param request
     * @param jlbh
     * @param //
     * @return
     */
    public ResultMsg savePcLyqO2Obj(HttpServletRequest request,String jlbh,String O2ObjJson){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);

            O2Obj o2obj = JSON.parseObject(O2ObjJson, O2Obj.class);

            // 保存数据
            SampleLhq oldData = sampleLhqDao.getPcSampleByJlbh(jlbh);
            oldData.o2Obj =o2obj;
            sampleLhqDao.save(oldData);


            //生成报告
            ReportLhq reportLhq = new ReportLhq();

            reportLhq.sampTYpe = 4;

            reportLhq.bh = oldData.jlbh+"_o2";
            // 设备二维码
            reportLhq.erweima =  oldData.erweima;
            // 二维码的头部
            reportLhq.bsbhHrader =  oldData.bsbhHrader;
            // 出厂编号
            reportLhq.ccbh =  oldData.ccbh;
            // 记录编号  唯一
            reportLhq.jlbh =  oldData.jlbh;
            // 送检单位
            reportLhq.sjdw =  oldData.sjdw;
            // 仪器名称
            reportLhq.yqmc =  oldData.yqmc;
            // 制造单位
            reportLhq.zzdw =  oldData.zzdw;
            // 仪器型号
            reportLhq.yqxh =  oldData.yqxh;
            //传感器类型
            reportLhq.cgqlx =  oldData.cgqlx;
            // 记录人id
            reportLhq.jlrId =  oldData.jlrId;
            // 记录人
            reportLhq.jlr =  oldData.jlr;
            // 检定地址
            reportLhq.jddz =  oldData.jddz;
            //其他信息
            reportLhq.qt_xx = oldData.qt_xx;
            // 1是  -1 否   0默认没选
            reportLhq.isH2S =  oldData.isH2S;
            // 1是  -1 否   0默认没选
            reportLhq.isO2 =  oldData.isO2;
            // 1是  -1 否   0默认没选
            reportLhq.isKrq =  oldData.isKrq;
            // 1是  -1 否   0默认没选
            reportLhq.isCo =  oldData.isCo;
            reportLhq.jdyj1 =  oldData.jdyj1;
            reportLhq.jdyj2 =  oldData.jdyj2;
            reportLhq.jdyj3 =  oldData.jdyj3;
            reportLhq.jdyj4 =  oldData.jdyj4;
            reportLhq.jdzz1 =  oldData.jdzz1;
            reportLhq.jdzz2 =  oldData.jdzz2;
            reportLhq.jdzz3 =  oldData.jdzz3;
            reportLhq.jdzz4 =  oldData.jdzz4;
            reportLhq.sjrq =  oldData.sjrq;

            //设置检验日期
            reportLhq.jyrq =  TimeUtil.formatTime_tow(TimeUtil.currentTimeMillis());

            Date date2 = new Date(TimeUtil.currentTimeMillis());
            long year3 = TimeUtil.getDateBeforeYear(date2, 1);
            //下次检验日期
            reportLhq.xcjyrq = TimeUtil.formatTime_tow(year3);

            reportLhq.jyyId =user.getUserId()+"";
            reportLhq.jyy =user.getUserName();

            reportLhq.hyyId = "";
            reportLhq.hyy =  "";

            reportLhq.pzrId = "";
            reportLhq.pzr = "";


            O2ObjReport o2ObjReport = new O2ObjReport();
            //环境温度
            o2ObjReport.hjwd= o2obj.hjwd;
            //环境湿度
            o2ObjReport.hjsd= o2obj.hjsd;
            //外观与结构结果
            o2ObjReport.om_wgyjg_jg= o2obj.om_wgyjg_jg;
            //外观与结构 备注
            o2ObjReport.om_wgyjg_bz= o2obj.om_wgyjg_bz;
            //标志和标识 结果om_
            o2ObjReport.od_bzhbs_jg= o2obj.od_bzhbs_jg;
            //标志和标识 备注om_
            o2ObjReport.od_bzhbs_bz= o2obj.od_bzhbs_bz;
            //通电检查 结果
            //通电检查  备注
            //结缘电阻 结果
            o2ObjReport.om_jyqd_jg= o2obj.om_jyqd_jg;
            o2ObjReport.om_jydz_jg= o2obj.om_jydz_jg;
            o2ObjReport.om_jyqd_jg= o2obj.om_jyqd_jg;
            o2ObjReport.om_jyqd_bz= o2obj.om_jyqd_bz;
            //结缘电阻  备注
            o2ObjReport.om_jydz_bz= o2obj.om_jydz_bz;
            o2ObjReport.o_yqmlc= o2obj.o_yqmlc;//示值误差-仪器满量程
            o2ObjReport.om_swcz_bq1= o2obj.om_swcz_bq1;//示值误差-标气浓度1
            o2ObjReport.om_swcz_bq2= o2obj.om_swcz_bq2;//示值误差-标气浓度2
            o2ObjReport.om_swcz_bq3= o2obj.om_swcz_bq3;//示值误差-标气浓度3
            o2ObjReport.om_swcz_x1= o2obj.om_swcz_x1;
            o2ObjReport.om_swcz_x2= o2obj.om_swcz_x2;
            o2ObjReport.om_swcz_x3= o2obj.om_swcz_x3;
            o2ObjReport.om_swcz_y1= o2obj.om_swcz_y1;
            o2ObjReport.om_swcz_y2= o2obj.om_swcz_y2;
            o2ObjReport.om_swcz_y3= o2obj.om_swcz_y3;
            o2ObjReport.om_swcz_z1= o2obj.om_swcz_z1;
            o2ObjReport.om_swcz_z2= o2obj.om_swcz_z2;
            o2ObjReport.om_swcz_z3= o2obj.om_swcz_z3;
            o2ObjReport.om_swcz_pjz1= o2obj.om_swcz_pjz1;//示值误差-算术平均值1
            o2ObjReport.om_swcz_pjz2= o2obj.om_swcz_pjz2;//示值误差-算术平均值2
            o2ObjReport.om_swcz_pjz3= o2obj.om_swcz_pjz3;//示值误差-算术平均值3
            o2ObjReport.om_swcz_cldszwc1= o2obj.om_swcz_cldszwc1;//示值误差-示值误差1
            o2ObjReport.om_swcz_cldszwc2= o2obj.om_swcz_cldszwc2;//示值误差-示值误差2
            o2ObjReport.om_swcz_cldszwc3= o2obj.om_swcz_cldszwc3;//示值误差-示值误差3
            o2ObjReport.om_swcz_cldjdwc1= o2obj.om_swcz_cldjdwc1;//示值误差-相对误差值1
            o2ObjReport.om_swcz_cldjdwc2= o2obj.om_swcz_cldjdwc2;//示值误差-相对误差值2
            o2ObjReport.om_swcz_cldjdwc3= o2obj.om_swcz_cldjdwc3;//示值误差-相对误差值3
            o2ObjReport.om_swcz_yqszwc= o2obj.om_swcz_yqszwc;
            o2ObjReport.o_xysj_yqlc= o2obj.o_xysj_yqlc;//响应时间- 标气浓度
            o2ObjReport.om_xysj_x1= o2obj.om_xysj_x1;//
            o2ObjReport.om_xysj_x2= o2obj.om_xysj_x2;//
            o2ObjReport.om_xysj_x3= o2obj.om_xysj_x3;//
            o2ObjReport.om_xysj_xysj= o2obj.om_xysj_xysj;//响应时间-算术平均值
            o2ObjReport.om_cfx_yqlc= o2obj.om_cfx_yqlc;//重复性-标气浓度
            o2ObjReport.om_cfx_x1= o2obj.om_cfx_x1;//
            o2ObjReport.om_cfx_x2= o2obj.om_cfx_x2;//
            o2ObjReport.om_cfx_x3= o2obj.om_cfx_x3;//
            o2ObjReport.om_cfx_x4= o2obj.om_cfx_x4;//
            o2ObjReport.om_cfx_x5= o2obj.om_cfx_x5;//
            o2ObjReport.om_cfx_x6= o2obj.om_cfx_x6;//
            o2ObjReport.om_cfx_cfx= o2obj.om_cfx_cfx;//重复性-重复性
            o2ObjReport.om_cfx_pjz= o2obj.om_cfx_pjz;//重复性-重复性
            o2ObjReport.om_py_r=o2obj.om_py_r;//漂移-仪器满量程
            o2ObjReport.om_py_x0= o2obj.om_py_x0;//
            o2ObjReport.om_py_x1= o2obj.om_py_x1;//
            o2ObjReport.om_py_x2= o2obj.om_py_x2;//
            o2ObjReport.om_py_x3= o2obj.om_py_x3;//
            o2ObjReport.om_py_x4= o2obj.om_py_x4;//

            o2ObjReport.om_py_y0= o2obj.om_py_y0;//
            o2ObjReport.om_py_y1= o2obj.om_py_y1;//
            o2ObjReport.om_py_y2= o2obj.om_py_y2;//
            o2ObjReport.om_py_y3= o2obj.om_py_y3;//
            o2ObjReport.om_py_y4= o2obj.om_py_y4;//

            o2ObjReport.om_py_ldpy= o2obj.om_py_ldpy;//漂移-零点漂移
            o2ObjReport.om_py_lcpy= o2obj.om_py_lcpy;//漂移-量程漂移


            String bhgyy="";
            //判断是否合格
            String m_reportStatue="合格";
            if("不合格".equals(o2obj.om_wgyjg_jg)){
                m_reportStatue = "不合格";
                bhgyy=bhgyy+"外观与结构"+",";
            }

            if("不合格".equals(o2obj.od_bzhbs_jg)){
                m_reportStatue = "不合格";
                bhgyy=bhgyy+"标志与标识"+",";
            }



            if("不合格".equals(o2obj.om_jydz_jg)){
                m_reportStatue = "不合格";
                bhgyy=bhgyy+"结缘电阻"+",";
            }

            if("不合格".equals(o2obj.om_jyqd_jg)){
                m_reportStatue = "不合格";
                bhgyy=bhgyy+"结缘强度"+",";
            }


            double mzdz=0;
            if (o2obj.o_yqmlc<=25){
                if (o2obj.om_swcz_yqszwc>-2 & o2obj.om_swcz_yqszwc<2){
                    o2ObjReport.swcz_pdjg="合格";
                }else {
                    o2ObjReport.swcz_pdjg="不合格";
                    m_reportStatue = "不合格";
                    bhgyy=bhgyy+"示值误差"+",";
                }
            }else {
                if (o2obj.om_swcz_yqszwc>-3 & o2obj.om_swcz_yqszwc<3){
                    o2ObjReport.swcz_pdjg="合格";
                }else {
                    o2ObjReport.swcz_pdjg="不合格";
                    m_reportStatue = "不合格";
                    bhgyy=bhgyy+"示值误差"+",";
                }
            }

            switch (oldData.cgqlx) {
                case "-1":
                    if (o2obj.om_xysj_xysj <= 30) {
                        o2ObjReport.xysj_pdjg = "合格";
                    } else {
                        o2ObjReport.xysj_pdjg = "不合格";
                        m_reportStatue = "不合格";
                        bhgyy=bhgyy+"响应时间"+",";
                    }
                    break;
                case "1":
                    if (o2obj.om_xysj_xysj <= 60) {
                        o2ObjReport.xysj_pdjg = "合格";
                    } else {
                        o2ObjReport.xysj_pdjg = "不合格";
                        m_reportStatue = "不合格";
                        bhgyy=bhgyy+"响应时间"+",";
                    }
                    break;
            }



            //重复性 判断
            if(o2obj.om_cfx_cfx <=1){
                o2ObjReport.cfx_pdjg = "合格";
            }else{
                o2ObjReport.cfx_pdjg = "不合格";
                m_reportStatue = "不合格";
                bhgyy=bhgyy+"重复性"+",";
            }

            //零点漂移
//            if(o2obj.om_py_ldpy <2 && o2obj.om_py_ldpy>(-2)){
//                o2ObjReport.ldpy_pdjg ="合格";
//            }else{
//                o2ObjReport.ldpy_pdjg ="不合格";
//                m_reportStatue = "不合格";
//                bhgyy=bhgyy+"零点漂移"+",";
//            }
//
//            //量程漂移
//            if(o2obj.om_py_lcpy <3 && o2obj.om_py_lcpy>(-3)){
//                o2ObjReport.lcpy_pdjg ="合格";
//            }else{
//                o2ObjReport.lcpy_pdjg ="不合格";
//                m_reportStatue = "不合格";
//                bhgyy=bhgyy+"量程漂移"+",";
//            }




            if(m_reportStatue.equals("合格")){
                reportLhq.reportStatue= 1;
            }else if(m_reportStatue.equals("不合格")){
                reportLhq.reportStatue= 2;
                String  bhgyy_Str ="";
                if(!"".equals(bhgyy)){
                    bhgyy_Str=bhgyy.substring(0,bhgyy.length()-1);
                }
                o2ObjReport.bhgyy = bhgyy_Str+"不合格";
            }

            reportLhq.reportProgress =1;

            reportLhq.o2ObjReport = o2ObjReport;
            reportLhqDao.save(reportLhq);
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("保存成功");
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }
    /**
     * 保存ch4 对象
     * @param request
     * @param jlbh
     * @param //
     * @return
     */
    public ResultMsg savePcLyqCh4Obj(HttpServletRequest request,String jlbh,String Ch4ObjJSon){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);

            CH4Obj ch4Obj = JSON.parseObject(Ch4ObjJSon,CH4Obj.class);

            // 保存数据
            SampleLhq oldData = sampleLhqDao.getPcSampleByJlbh(jlbh);
            oldData.ch4Obj =ch4Obj;
            sampleLhqDao.save(oldData);


            //生成报告
            ReportLhq reportLhq = new ReportLhq();

            reportLhq.sampTYpe = 3;

            reportLhq.bh = oldData.jlbh+"_ch4";
            // 设备二维码
            reportLhq.erweima =  oldData.erweima;
            // 二维码的头部
            reportLhq.bsbhHrader =  oldData.bsbhHrader;
            // 出厂编号
            reportLhq.ccbh =  oldData.ccbh;
            // 记录编号  唯一
            reportLhq.jlbh =  oldData.jlbh;
            // 送检单位
            reportLhq.sjdw =  oldData.sjdw;
            // 仪器名称
            reportLhq.yqmc =  oldData.yqmc;
            // 制造单位
            reportLhq.zzdw =  oldData.zzdw;
            // 仪器型号
            reportLhq.yqxh =  oldData.yqxh;
            //传感器类型
            reportLhq.cgqlx =  oldData.cgqlx;
            // 记录人id
            reportLhq.jlrId =  oldData.jlrId;
            // 记录人
            reportLhq.jlr =  oldData.jlr;
            // 检定地址
            reportLhq.jddz =  oldData.jddz;
            //其他信息
            reportLhq.qt_xx = oldData.qt_xx;
            // 1是  -1 否   0默认没选
            reportLhq.isH2S =  oldData.isH2S;
            // 1是  -1 否   0默认没选
            reportLhq.isO2 =  oldData.isO2;
            // 1是  -1 否   0默认没选
            reportLhq.isKrq =  oldData.isKrq;
            // 1是  -1 否   0默认没选
            reportLhq.isCo =  oldData.isCo;
            reportLhq.jdyj1 =  oldData.jdyj1;
            reportLhq.jdyj2 =  oldData.jdyj2;
            reportLhq.jdyj3 =  oldData.jdyj3;
            reportLhq.jdyj4 =  oldData.jdyj4;
            reportLhq.jdzz1 =  oldData.jdzz1;
            reportLhq.jdzz2 =  oldData.jdzz2;
            reportLhq.jdzz3 =  oldData.jdzz3;
            reportLhq.jdzz4 =  oldData.jdzz4;
            reportLhq.sjrq =  oldData.sjrq;

            //设置检验日期
            reportLhq.jyrq =  TimeUtil.formatTime_tow(TimeUtil.currentTimeMillis());

            Date date2 = new Date(TimeUtil.currentTimeMillis());
            long year3 = TimeUtil.getDateBeforeYear(date2, 1);
            //下次检验日期
            reportLhq.xcjyrq = TimeUtil.formatTime_tow(year3);

            reportLhq.jyyId =user.getUserId()+"";
            reportLhq.jyy =user.getUserName();

            reportLhq.hyyId = "";
            reportLhq.hyy =  "";

            reportLhq.pzrId = "";
            reportLhq.pzr = "";


            Ch4ObjReport ch4ObjReport = new Ch4ObjReport();
            //环境温度
            ch4ObjReport.hjwd= ch4Obj.hjwd;
            //环境湿度
            ch4ObjReport.hjsd= ch4Obj.hjsd;
            //外观与结构结果
            ch4ObjReport.ch_wgyjg_jg= ch4Obj.ch_wgyjg_jg;
            //外观与结构 备注
            ch4ObjReport.ch_wgyjg_bz= ch4Obj.ch_wgyjg_bz;
            //标志和标识 结果om_
            ch4ObjReport.ch_bzhbs_jg= ch4Obj.ch_bzhbs_jg;
            //标志和标识 备注om_
            ch4ObjReport.ch_bzhbs_bz= ch4Obj.ch_bzhbs_bz;
            //通电检查 结果
            ch4ObjReport.ch_tdjc_jg = ch4Obj.ch_tdjc_jg;
            //通电检查  备注
            ch4ObjReport.ch_tdjc_bz=ch4Obj.ch_tdjc_bz;
            //结缘电阻 结果
            ch4ObjReport.ch_jydz_jg= ch4Obj.ch_jydz_jg;
            //结缘电阻  备注
            ch4ObjReport.ch_jydz_bz= ch4Obj.ch_jydz_bz;
            ch4ObjReport.ch_yqmlc= ch4Obj.ch_yqmlc;//示值误差-仪器满量程
            ch4ObjReport.ch_swcz_bq1= ch4Obj.ch_swcz_bq1;//示值误差-标气浓度1
            ch4ObjReport.ch_swcz_bq2= ch4Obj.ch_swcz_bq2;//示值误差-标气浓度2
            ch4ObjReport.ch_swcz_bq3= ch4Obj.ch_swcz_bq3;//示值误差-标气浓度3
            ch4ObjReport.ch_scbjz1= ch4Obj.ch_scbjz1;
            ch4ObjReport.ch_scbjz2= ch4Obj.ch_scbjz2;
            ch4ObjReport.ch_scbjz3= ch4Obj.ch_scbjz3;
            ch4ObjReport.ch_bjz= ch4Obj.ch_bjz;
            ch4ObjReport.ch_bjgn_jg= ch4Obj.ch_bjgn_jg;
            ch4ObjReport.ch_bjndpd_jg=ch4Obj.ch_bjndpd_jg;

            ch4ObjReport.ch_swcz_x1= ch4Obj.ch_swcz_x1;
            ch4ObjReport.ch_swcz_x2= ch4Obj.ch_swcz_x2;
            ch4ObjReport.ch_swcz_x3= ch4Obj.ch_swcz_x3;
            ch4ObjReport.ch_swcz_y1= ch4Obj.ch_swcz_y1;
            ch4ObjReport.ch_swcz_y2= ch4Obj.ch_swcz_y2;
            ch4ObjReport.ch_swcz_y3= ch4Obj.ch_swcz_y3;
            ch4ObjReport.ch_swcz_z1= ch4Obj.ch_swcz_z1;
            ch4ObjReport.ch_swcz_z2= ch4Obj.ch_swcz_z2;
            ch4ObjReport.ch_swcz_z3= ch4Obj.ch_swcz_z3;
            ch4ObjReport.ch_swcz_pjz1= ch4Obj.ch_swcz_pjz1;//示值误差-算术平均值1
            ch4ObjReport.ch_swcz_pjz2= ch4Obj.ch_swcz_pjz2;//示值误差-算术平均值2
            ch4ObjReport.ch_swcz_pjz3= ch4Obj.ch_swcz_pjz3;//示值误差-算术平均值3

            ch4ObjReport.ch_swcz_cldszwc1= ch4Obj.ch_swcz_cldszwc1;//示值误差-测量点示值误差值1
            ch4ObjReport.ch_swcz_cldszwc2= ch4Obj.ch_swcz_cldszwc2;//示值误差-测量点示值误差值2
            ch4ObjReport.ch_swcz_cldszwc3= ch4Obj.ch_swcz_cldszwc3;//示值误差-测量点示值误差值3
            ch4ObjReport.ch_swcz_yqszwc= ch4Obj.ch_swcz_yqszwc;

            ch4ObjReport.ch_xysj_yqlc= ch4Obj.ch_xysj_yqlc;//响应时间- 标气浓度
            ch4ObjReport.ch_xysj_x1= ch4Obj.ch_xysj_x1;//
            ch4ObjReport.ch_xysj_x2= ch4Obj.ch_xysj_x2;//
            ch4ObjReport.ch_xysj_x3= ch4Obj.ch_xysj_x3;//
            ch4ObjReport.ch_xysj_pjz= ch4Obj.ch_xysj_pjz;//响应时间-算术平均值
            ch4ObjReport.ch_cfx_bq= ch4Obj.ch_cfx_bq;//重复性-标气浓度
            ch4ObjReport.ch_cfx_x1= ch4Obj.ch_cfx_x1;//
            ch4ObjReport.ch_cfx_x2= ch4Obj.ch_cfx_x2;//
            ch4ObjReport.ch_cfx_x3= ch4Obj.ch_cfx_x3;//
            ch4ObjReport.ch_cfx_x4= ch4Obj.ch_cfx_x4;//
            ch4ObjReport.ch_cfx_x5= ch4Obj.ch_cfx_x5;//
            ch4ObjReport.ch_cfx_x6= ch4Obj.ch_cfx_x6;//
            ch4ObjReport.ch_cfx_cfx= ch4Obj.ch_cfx_cfx;//重复性-重复性
            ch4ObjReport.ch_py_yqmlc=ch4Obj.ch_py_yqmlc;//漂移-仪器满量程
            ch4ObjReport.ch_py_x0= ch4Obj.ch_py_x0;//
            ch4ObjReport.ch_py_x1= ch4Obj.ch_py_x1;//
            ch4ObjReport.ch_py_x2= ch4Obj.ch_py_x2;//
            ch4ObjReport.ch_py_x3= ch4Obj.ch_py_x3;//
            ch4ObjReport.ch_py_x4= ch4Obj.ch_py_x4;//
            ch4ObjReport.ch_py_x5= ch4Obj.ch_py_x5;//
            ch4ObjReport.ch_py_x6= ch4Obj.ch_py_x6;//
            ch4ObjReport.ch_py_y0= ch4Obj.ch_py_y0;//
            ch4ObjReport.ch_py_y1= ch4Obj.ch_py_y1;//
            ch4ObjReport.ch_py_y2= ch4Obj.ch_py_y2;//
            ch4ObjReport.ch_py_y3= ch4Obj.ch_py_y3;//
            ch4ObjReport.ch_py_y4= ch4Obj.ch_py_y4;//
            ch4ObjReport.ch_py_y5= ch4Obj.ch_py_y5;//
            ch4ObjReport.ch_py_y6= ch4Obj.ch_py_y6;//
            ch4ObjReport.ch_py_ldpy= ch4Obj.ch_py_ldpy;//漂移-零点漂移
            ch4ObjReport.ch_py_lcpy= ch4Obj.ch_py_lcpy;//漂移-量程漂移


            String bhgyy="";
            //判断是否合格
            String m_reportStatue="合格";
            if("不合格".equals(ch4Obj.ch_wgyjg_jg)){
                m_reportStatue = "不合格";
                bhgyy=bhgyy+"外观与结构"+",";
            }

            if("不合格".equals(ch4Obj.ch_bzhbs_jg)){
                m_reportStatue = "不合格";
                bhgyy=bhgyy+"标志与标识"+",";
            }



//            if("不合格".equals(o2obj.jydz_jg)){
//                m_reportStatue = "不合格";
//                bhgyy=bhgyy+"结缘电阻"+",";
//            }
            if("不合格".equals(ch4Obj.ch_tdjc_jg)){
                m_reportStatue = "不合格";
                bhgyy=bhgyy+"通电检查"+",";
            }
            if("不合格".equals(ch4Obj.ch_bjgn_jg)){
                m_reportStatue = "不合格";

                bhgyy=bhgyy+"报警功能"+",";
            }

            if("不合格".equals(ch4Obj.ch_bjndpd_jg)){
                m_reportStatue = "不合格";
                bhgyy=bhgyy+"报警浓度值"+",";
            }


                if (ch4Obj.ch_swcz_yqszwc>-5 & ch4Obj.ch_swcz_yqszwc<5){
                    ch4ObjReport.swcz_pdjg="合格";
                }else {
                    ch4ObjReport.swcz_pdjg="不合格";
                    m_reportStatue = "不合格";

                    bhgyy=bhgyy+"示值误差"+",";
                }


            switch (oldData.cgqlx) {
                case "-1":
                    if (ch4Obj.ch_xysj_pjz <= 30) {
                        ch4ObjReport.xysj_pdjg = "合格";
                    } else {
                        ch4ObjReport.xysj_pdjg = "不合格";
                        m_reportStatue = "不合格";
                        bhgyy=bhgyy+"响应时间"+",";
                    }
                    break;
                case "1":
                    if (ch4Obj.ch_xysj_pjz <= 60) {
                        ch4ObjReport.xysj_pdjg = "合格";

                    } else {
                        ch4ObjReport.xysj_pdjg = "不合格";
                        m_reportStatue = "不合格";
                        bhgyy=bhgyy+"响应时间"+",";
                    }
                    break;
            }



            //重复性 判断
            if(ch4Obj.ch_cfx_cfx <=2){
                ch4ObjReport.cfx_pdjg = "合格";
            }else{
                ch4ObjReport.cfx_pdjg = "不合格";
                m_reportStatue = "不合格";
                bhgyy=bhgyy+"重复性"+",";
            }

            //零点漂移
//            if(o2obj.om_py_ldpy <2 && o2obj.om_py_ldpy>(-2)){
//                o2ObjReport.ldpy_pdjg ="合格";
//            }else{
//                o2ObjReport.ldpy_pdjg ="不合格";
//                m_reportStatue = "不合格";
//                bhgyy=bhgyy+"零点漂移"+",";
//            }
//
//            //量程漂移
//            if(o2obj.om_py_lcpy <3 && o2obj.om_py_lcpy>(-3)){
//                o2ObjReport.lcpy_pdjg ="合格";
//            }else{
//                o2ObjReport.lcpy_pdjg ="不合格";
//                m_reportStatue = "不合格";
//                bhgyy=bhgyy+"量程漂移"+",";
//            }




            if(m_reportStatue.equals("合格")){
                reportLhq.reportStatue= 1;
            }else if(m_reportStatue.equals("不合格")){
                reportLhq.reportStatue= 2;
                String  bhgyy_Str ="";
                if(!"".equals(bhgyy)){
                    bhgyy_Str=bhgyy.substring(0,bhgyy.length()-1);
                }
                ch4ObjReport.bhgyy = bhgyy_Str+"不合格";
            }

            reportLhq.reportProgress =1;

            reportLhq.ch4ObjReport = ch4ObjReport;
            reportLhqDao.save(reportLhq);
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("保存成功");
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 保存CO 对象
     * @param request
     * @param jlbh
     * @param coObjJson
     * @return
     */
    public ResultMsg savePcLyqCoObj(HttpServletRequest request,String jlbh,String coObjJson){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            CoObj coObj = JSON.parseObject(coObjJson, CoObj.class);

            // 保存数据
            SampleLhq oldData = sampleLhqDao.getPcSampleByJlbh(jlbh);
            oldData.coObj = coObj;
            sampleLhqDao.save(oldData);

            //生成报告
            ReportLhq reportLhq = new ReportLhq();
            reportLhq.sampTYpe = 2;
            reportLhq.bh = oldData.jlbh+"_co";
            // 设备二维码
            reportLhq.erweima =  oldData.erweima;
            // 二维码的头部
            reportLhq.bsbhHrader =  oldData.bsbhHrader;
            // 出厂编号
            reportLhq.ccbh =  oldData.ccbh;
            // 记录编号  唯一
            reportLhq.jlbh =  oldData.jlbh;
            // 送检单位
            reportLhq.sjdw =  oldData.sjdw;
            // 仪器名称
            reportLhq.yqmc =  oldData.yqmc;
            // 制造单位
            reportLhq.zzdw =  oldData.zzdw;
            // 仪器型号
            reportLhq.yqxh =  oldData.yqxh;
            //传感器类型
            reportLhq.cgqlx =  oldData.cgqlx;
            // 记录人id
            reportLhq.jlrId =  oldData.jlrId;
            // 记录人
            reportLhq.jlr =  oldData.jlr;
            // 检定地址
            reportLhq.jddz =  oldData.jddz;
            //其他信息
            reportLhq.qt_xx = oldData.qt_xx;
            // 1是  -1 否   0默认没选
            reportLhq.isH2S =  oldData.isH2S;
            // 1是  -1 否   0默认没选
            reportLhq.isO2 =  oldData.isO2;
            // 1是  -1 否   0默认没选
            reportLhq.isKrq =  oldData.isKrq;
            // 1是  -1 否   0默认没选
            reportLhq.isCo =  oldData.isCo;
            reportLhq.jdyj1 =  oldData.jdyj1;
            reportLhq.jdyj2 =  oldData.jdyj2;
            reportLhq.jdyj3 =  oldData.jdyj3;
            reportLhq.jdyj4 =  oldData.jdyj4;
            reportLhq.jdzz1 =  oldData.jdzz1;
            reportLhq.jdzz2 =  oldData.jdzz2;
            reportLhq.jdzz3 =  oldData.jdzz3;
            reportLhq.jdzz4 =  oldData.jdzz4;
            reportLhq.sjrq =  oldData.sjrq;
            //设置检验日期
            reportLhq.jyrq =  TimeUtil.formatTime_tow(TimeUtil.currentTimeMillis());
            Date date2 = new Date(TimeUtil.currentTimeMillis());
            long year3 = TimeUtil.getDateBeforeYear(date2, 1);
            //下次检验日期
            reportLhq.xcjyrq = TimeUtil.formatTime_tow(year3);
            reportLhq.jyyId =user.getUserId()+"";
            reportLhq.jyy =user.getUserName();
            reportLhq.hyyId = "";
            reportLhq.hyy =  "";
            reportLhq.pzrId = "";
            reportLhq.pzr = "";


            CoObjReport coObjReport = new CoObjReport();
            //环境温度
            coObjReport.hjwd= coObj.hjwd;
            //环境湿度
            coObjReport.hjsd= coObj.hjsd;
            //外观与结构结果
            coObjReport.wgyjg_jg= coObj.wgyjg_jg;
            //外观与结构 备注
            coObjReport.wgyjg_bz= coObj.wgyjg_bz;
            //标志和标识 结果
            coObjReport.bzhbs_jg= coObj.bzhbs_jg;
            //标志和标识 备注
            coObjReport.bzhbs_bz= coObj.bzhbs_bz;
            //通电检查 结果
            coObjReport.tdjc_jg= coObj.tdjc_jg;
            //通电检查  备注
            coObjReport.tdjc_bz= coObj.tdjc_bz;

            //声光报警结果
            coObjReport.sgbj_jg = coObj.sgbj_jg;
            //报警浓度值
            coObjReport.bjndz = coObj.bjndz;

            coObjReport.co_bjndz_jg = coObj.co_bjndz_jg;

            coObjReport.co_swcz_bq1= coObj.co_swcz_bq1;//示值误差-标气浓度1
            coObjReport.co_swcz_bq2= coObj.co_swcz_bq2;//示值误差-标气浓度2
            coObjReport.co_swcz_bq3= coObj.co_swcz_bq3;//示值误差-标气浓度3
            coObjReport.co_swcz_x1= coObj.co_swcz_x1;
            coObjReport.co_swcz_x2= coObj.co_swcz_x2;
            coObjReport.co_swcz_x3= coObj.co_swcz_x3;
            coObjReport.co_swcz_y1= coObj.co_swcz_y1;
            coObjReport.co_swcz_y2= coObj.co_swcz_y2;
            coObjReport.co_swcz_y3= coObj.co_swcz_y3;
            coObjReport.co_swcz_z1= coObj.co_swcz_z1;
            coObjReport.co_swcz_z2= coObj.co_swcz_z2;
            coObjReport.co_swcz_z3= coObj.co_swcz_z3;
            coObjReport.co_swcz_pjz1= coObj.co_swcz_pjz1;//示值误差-算术平均值1
            coObjReport.co_swcz_pjz2= coObj.co_swcz_pjz2;//示值误差-算术平均值2
            coObjReport.co_swcz_pjz3= coObj.co_swcz_pjz3;//示值误差-算术平均值3

            coObjReport.co_swcz_jdwcz1= coObj.co_swcz_jdwcz1;//示值误差-测量点绝对误差
            coObjReport.co_swcz_jdwcz2= coObj.co_swcz_jdwcz2;//示值误差-测量点绝对误差
            coObjReport.co_swcz_jdwcz3= coObj.co_swcz_jdwcz3;//示值误差-测量点绝对误差

            coObjReport.co_swcz_xdwcz1= coObj.co_swcz_xdwcz1;//示值误差-相对误差值1
            coObjReport.co_swcz_xdwcz2= coObj.co_swcz_xdwcz2;//示值误差-相对误差值2
            coObjReport.co_swcz_xdwcz3= coObj.co_swcz_xdwcz3;//示值误差-相对误差值3
            coObjReport.co_yq_jdwcz = coObj.co_yq_jdwcz;
            coObjReport.co_yq_xdwcz = coObj.co_yq_xdwcz;


            coObjReport.co_xysj_x1 = coObj.co_xysj_x1;//响应时间
            coObjReport.co_xysj_x2 = coObj.co_xysj_x2;//响应时间
            coObjReport.co_xysj_x3 = coObj.co_xysj_x3;//响应时间
            coObjReport.co_xysj_pjz = coObj.co_xysj_pjz;//响应时间 - 平均值


            coObjReport.co_cfx_x1 = coObj.co_cfx_x1;//重复性
            coObjReport.co_cfx_x2 = coObj.co_cfx_x2;//重复性
            coObjReport.co_cfx_x3 = coObj.co_cfx_x3;//重复性
            coObjReport.co_cfx_x4 = coObj.co_cfx_x4;//重复性
            coObjReport.co_cfx_x5 = coObj.co_cfx_x5;//重复性
            coObjReport.co_cfx_x6 = coObj.co_cfx_x6;//重复性
            coObjReport.co_cfx_pjz= coObj.co_cfx_pjz; //重复性 - 平均值
            coObjReport.co_cfx_cfx= coObj.co_cfx_cfx; //重复性 - 重复性


            coObjReport.co_py_x0 = coObj.co_py_x0;//漂移 - 零点
            coObjReport.co_py_x1 = coObj.co_py_x1;//漂移 - 零点
            coObjReport.co_py_x2 = coObj.co_py_x2;//漂移 - 零点
            coObjReport.co_py_x3 = coObj.co_py_x3;//漂移 - 零点
            coObjReport.co_py_x4 = coObj.co_py_x4;//漂移 - 零点

            coObjReport.co_py_y0 = coObj.co_py_y0;//漂移 - 示值
            coObjReport.co_py_y1 = coObj.co_py_y1;//漂移 - 示值
            coObjReport.co_py_y2 = coObj.co_py_y2;//漂移 - 示值
            coObjReport.co_py_y3 = coObj.co_py_y3;//漂移 - 示值
            coObjReport.co_py_y4 = coObj.co_py_y4;//漂移 - 示值

            coObjReport.co_py_ldpy = coObj.co_py_ldpy; //零点漂移
            coObjReport.co_py_lcpy = coObj.co_py_lcpy; //量程漂移

            String bhgyy="";
            //判断是否合格
            String co_reportStatue="合格";


            if("不合格".equals(coObjReport.wgyjg_jg)){
                co_reportStatue = "不合格";
                bhgyy=bhgyy+"外观与结构"+",";
            }

            if("不合格".equals(coObjReport.bzhbs_jg)){
                co_reportStatue = "不合格";
                bhgyy=bhgyy+"标志和标识"+",";
            }

            if("不合格".equals(coObjReport.tdjc_jg)){
                co_reportStatue = "不合格";
                bhgyy=bhgyy+"通电检查"+",";
            }


            if("不合格".equals(coObjReport.sgbj_jg)){
                co_reportStatue = "不合格";
                bhgyy=bhgyy+"报警功能"+",";
            }

            if("不合格".equals(coObjReport.co_bjndz_jg)){
                co_reportStatue = "不合格";
                bhgyy=bhgyy+"报警浓度值"+",";

            }


            //示误差值判断
            if(coObjReport.co_yq_jdwcz>(-5) && coObjReport.co_yq_jdwcz<5 || coObjReport.co_yq_xdwcz>(-10) && coObjReport.co_yq_xdwcz<10){
                coObjReport.swcz_pdjg="合格";

                if(coObjReport.co_yq_jdwcz>(-5) && coObjReport.co_yq_jdwcz<5){
                    coObjReport.szwc_hgx = coObjReport.co_yq_jdwcz+"";
                }
                if(coObjReport.co_yq_xdwcz>(-10) && coObjReport.co_yq_xdwcz<10){
                    coObjReport.szwc_hgx = coObjReport.co_yq_xdwcz+"";
                }
            }else{
                co_reportStatue = "不合格";
                bhgyy=bhgyy+"示误差值"+",";

                if(coObjReport.co_yq_jdwcz<(-5) || coObjReport.co_yq_jdwcz>5){
                    coObjReport.szwc_hgx = coObjReport.co_yq_jdwcz+"";
                }
                if(coObjReport.co_yq_xdwcz<(-10) || coObjReport.co_yq_xdwcz>10){
                    coObjReport.szwc_hgx = coObjReport.co_yq_xdwcz+"";
                }

            }

            //响应时间判断
            //如果是扩散式
            if(oldData.cgqlx.equals("1")){
                if(coObjReport.co_xysj_pjz<=60){
                    coObjReport.xysj_pdjg ="合格";
                }else{
                    coObjReport.xysj_pdjg ="不合格";
                    co_reportStatue="不合格";
                    bhgyy=bhgyy+"响应时间"+",";
                }
            }else if(oldData.cgqlx.equals("-1")){  //吸入式
                if(coObjReport.co_xysj_pjz<=30){
                    coObjReport.xysj_pdjg ="合格";
                }else{
                    coObjReport.xysj_pdjg ="不合格";
                    co_reportStatue="不合格";
                    bhgyy=bhgyy+"响应时间"+",";
                }
            }
            //重复性
            if(coObjReport.co_cfx_cfx<=2){
                coObjReport.cfx_pdjg ="合格";
            }else{
                coObjReport.cfx_pdjg ="不合格";
                co_reportStatue="不合格";
                bhgyy=bhgyy+"重复性"+",";
            }

//            //零点漂移
//            if(coObjReport.co_py_ldpy <3 && coObjReport.co_py_ldpy>(-3)){
//                coObjReport.ldpy_pdjg ="合格";
//            }else{
//                coObjReport.ldpy_pdjg ="不合格";
//                co_reportStatue = "不合格";
//                bhgyy=bhgyy+"零点漂移"+",";
//            }
//
//            //量程漂移
//            if(coObjReport.co_py_lcpy <3 && coObjReport.co_py_lcpy>(-3)){
//                coObjReport.lcpy_pdjg ="合格";
//            }else{
//                coObjReport.lcpy_pdjg ="不合格";
//                co_reportStatue = "不合格";
//                bhgyy=bhgyy+"量程漂移"+",";
//            }

            if(co_reportStatue.equals("合格")){
                reportLhq.reportStatue= 1;
            }else if(co_reportStatue.equals("不合格")){
                reportLhq.reportStatue= 2;
                String  bhgyy_Str ="";
                if(!"".equals(bhgyy)){
                    bhgyy_Str=bhgyy.substring(0,bhgyy.length()-1);
                }
                coObjReport.bhgyy = bhgyy_Str+"不合格";
            }
            reportLhq.reportProgress =1;
            reportLhq.coObjReport = coObjReport;
            reportLhqDao.save(reportLhq);
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("保存成功");

        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }




}
